package ar.com.digitalbricks.view.imprenta.contacto.action;

import java.util.Collection;

import ar.com.digitalbricks.view.imprenta.helper.ContactoView;

import com.opensymphony.xwork2.Action;

public class EditarContacto extends CrearContacto{
	private Integer idContacto;
	private Integer idRandom;
	private String params;

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		this.getClienteView().setContactos((Collection<ContactoView>) session.get("contactos"));
		for (ContactoView contactoView : this.getClienteView().getContactos()) {
			if (this.esEste(contactoView)){
				contactoView.setCalendario(this.getContactoView().getCalendario());
				contactoView.setMail(this.getContactoView().getMail());
				contactoView.setNombre(this.getContactoView().getNombre());
				contactoView.setSeccion(this.getContactoView().getSeccion());
				contactoView.setTelefonos(this.getContactoView().getTelefonos());
				break;
			}
		}
		session.put("contactos", this.getClienteView().getContactos());
		this.setShowContacto(false);
		return Action.SUCCESS;
	}

	private boolean esEste(ContactoView contactoView) {
		return (this.getContactoView().getRandomId() != null 
				&& 
				this.getContactoView().getRandomId().equals(contactoView.getRandomId())) 
			|| 
				(this.getContactoView().getId() != null 
				&& 
				this.getContactoView().getId().equals(contactoView.getId()));
	}

	@SuppressWarnings("unchecked")
	@Override
	public String preparar() throws Exception {
		if (this.getParams() != null){
			this.cargarIds();
		}else{
			this.setMsg("contacto.no.seleccionado.grilla");
			return Action.INPUT;
		}
		if (this.getIdContacto() != null){
			this.setContactoView(this.getContactoMgr().getById(this.getIdContacto()));
		}else{
			Collection<ContactoView> contactos = (Collection<ContactoView>) session.get("contactos");
			for (ContactoView contactoView : contactos) {
				if (this.getIdRandom().equals(contactoView.getRandomId())){
					this.setContactoView(contactoView);
					break;
				}
			}
		}
		this.setShowContacto(true);
		return Action.INPUT;
	}

	private void cargarIds() {
		if (this.getParams().contains("idContacto")){
			int l = this.getParams().length();
			char[] c = new char[l];
			for(int i = 0; i < l; i++){
				c[i] = this.getParams().charAt(i);
			}
			this.idContacto = new Integer(String.copyValueOf(c, "idContacto_|_".length(), this.getParams().length() - "idContacto_|_".length()));
		}
		if (this.getParams().contains("idRandom")){
			int l = this.getParams().length();
			char[] c = new char[l];
			for(int i = 0; i < l; i++){
				c[i] = this.getParams().charAt(i);
			}
			this.idRandom = new Integer(String.copyValueOf(c, "idRandom_|_".length(), this.getParams().length() - "idRandom_|_".length()));
		}
	}

	public void setIdContacto(Integer idContacto) {
		this.idContacto = idContacto;
	}

	public Integer getIdContacto() {
		return this.idContacto;
	}
	
	public Integer getIdRandom() {
		return this.idRandom;
	}
	
	public void setIdRandom(Integer idRandom) {
		this.idRandom = idRandom;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public String getParams() {
		return this.params;
	}
}